package com.mooral.web.util;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;

import com.mooral.bean.Categories;
import com.mooral.bean.Category;

public class SorterUtils {

	public Categories arrangeCategoriesByDependency(Categories categories ){
		//Categories sortedCategories = (Categories)this.clone();
		
		// Se ordena por Nombre
		Collections.sort(categories,new CategoryComparatorByName());
		
		// Se crea un hashmap con las categorias
		HashMap<Long, Category> categoriesHM = new HashMap<Long, Category>();
		for(int i = 0 ; i < categories.size() ; i++ ){
			Category category = (Category) categories.get(i);
			categoriesHM.put((Long)category.get("id"), category);
		}
		
		// Asigno los hijos a las categorias
		for(int i = 0 ; i < categories.size() ; i++ ){
			Category category = (Category) categories.get(i);
			//if(category.get("parentId") != null && !category.get("parentId").equals("")) {
			if(category.get("parentId") != null ) {
				Category categoryParent = categoriesHM.get(category.get("parentId"));
				categoryParent.addChild(category);
			}
		}

		Categories sortedCategories = new Categories();
		// Creo las categorias ordenadas
		for(int i = 0 ; i < categories.size() ; i++ ){
			Category category = (Category) categories.get(i);
			if(category.get("parentId") == null){
				sortedCategories.add(category);
				sortedCategories.addAll(getAllChilds(category, ""));
			}
		}
		
		return sortedCategories;
	}
	
	public Categories sortCategoriesByDependency(Categories categories ){
		//Categories sortedCategories = (Categories)this.clone();
		
		// Se ordena por Nombre
		Collections.sort(categories,new CategoryComparatorByName());
		
		// Se crea un hashmap con las categorias
		HashMap<Long, Category> categoriesHM = new HashMap<Long, Category>();
		for(int i = 0 ; i < categories.size() ; i++ ){
			Category category = (Category) categories.get(i);
			categoriesHM.put((Long)category.get("id"), category);
		}
		
		// Asigno los hijos a las categorias
		for(int i = 0 ; i < categories.size() ; i++ ){
			Category category = (Category) categories.get(i);
			//if(category.get("parentId") != null && !category.get("parentId").equals("")) {
			if(category.get("parentId") != null ) {
				Category categoryParent = categoriesHM.get(category.get("parentId"));
				categoryParent.addChild(category);
			}
		}
		
		Categories sortedCategories = new Categories();
		// Creo las categorias ordenadas
		for(int i = 0 ; i < categories.size() ; i++ ){
			Category category = (Category) categories.get(i);
			if(category.get("parentId") == null){
				sortedCategories.add(category);
				sortedCategories.addAll(getAllChilds(category, "_"));
			}
		}
		
		return sortedCategories;
	}
	
	private Categories getAllChilds(Category category, String nameTab){
		Categories categories = new Categories();
		Iterator ite = category.getChilds().iterator(); 
		while (ite.hasNext()){
			Category child = (Category)ite.next();
			child.put("name", nameTab + child.get("name"));
			categories.add(child);
			categories.addAll(getAllChilds(child,nameTab + nameTab));
			
		}
		return categories; 
	}
	
	private class CategoryComparatorByParentId implements Comparator{
		@Override
		public int compare(Object cat1, Object cat2) {			
			int ret = 0;
			Long cat1ParentId = (Long)((Category)cat1).get("parentId");
			Long cat2ParentId = (Long)((Category)cat2).get("parentId");
			
			if(cat1ParentId == null && cat2ParentId == null) ret = 0;
			else if(cat1ParentId == null) ret = -1;
			else if(cat2ParentId == null) ret = +1;
			else ret = cat1ParentId.compareTo(cat2ParentId);

			return ret;
		}
	}	


	private class CategoryComparatorByName implements Comparator{

		@Override
		public int compare(Object cat1, Object cat2) {			
			return ((Category)cat1).get("name").toString().compareToIgnoreCase(((Category)cat2).get("name").toString());
		}
	}
	
	
	public String sortCategoriesByDependencyToHTML(Categories categories ){

		String categoriesToHTML = "";
		
		// Se ordena por Nombre
		Collections.sort(categories,new CategoryComparatorByName());
		
		// Se crea un hashmap con las categorias
		HashMap<Long, Category> categoriesHM = new HashMap<Long, Category>();
		for(int i = 0 ; i < categories.size() ; i++ ){
			Category category = (Category) categories.get(i);
			categoriesHM.put((Long)category.get("id"), category);
		}
		
		// Asigno los hijos a las categorias
		for(int i = 0 ; i < categories.size() ; i++ ){
			Category category = (Category) categories.get(i);
			//if(category.get("parentId") != null && !category.get("parentId").equals("")) {
			if(category.get("parentId") != null ) {
				Category categoryParent = categoriesHM.get(category.get("parentId"));
				categoryParent.addChild(category);
			}
		}
		
		Categories sortedCategories = new Categories();
		// Creo las categorias ordenadas
		for(int i = 0 ; i < categories.size() ; i++ ){
			Category category = (Category) categories.get(i);
			if(category.get("parentId") == null){
				
				categoriesToHTML += "<li><input type='checkbox' name='foo' value='whatever'/><label class='" + category.get("slug") + "'>" + category.get("name") + "</label>";

				sortedCategories.add(category);
				
				if(category.getChilds().size() > 0){
					categoriesToHTML += "<ul style='display:none;'>";
					categoriesToHTML += getAllChildsToHTML(category);
					categoriesToHTML += "</ul>";
				}
				categoriesToHTML += "</li>";
			}
		}
		
		return categoriesToHTML;
	}
	
	private String getAllChildsToHTML(Category category){
		String categoriesToHTML = "";

		Categories categories = new Categories();
		Iterator ite = category.getChilds().iterator(); 
		while (ite.hasNext()){
			Category child = (Category)ite.next();
			categoriesToHTML += "<li><input type='checkbox' name='foo' value='whatever'/><label class='" + child.get("slug") + "'>" + child.get("name") + "</label>";
			categories.add(child);
			if(child.getChilds().size() > 0){
				categoriesToHTML += "<ul style='display:none;'>";
				categoriesToHTML += getAllChildsToHTML(child);
				categoriesToHTML += "</ul>";
			}
			categoriesToHTML += "</li>";
			
		}
		return categoriesToHTML;
	}
	
	
}